package org.dromara.business.modules.searchHistory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.business.modules.searchHistory.domain.BuSearchHistory;
import org.dromara.business.modules.searchHistory.domain.bo.BuSearchHistoryBo;
import org.dromara.business.modules.searchHistory.domain.vo.BuSearchHistoryVo;
import org.dromara.business.modules.searchHistory.domain.vo.SearchHistoryVo;
import org.dromara.business.modules.searchHistory.domain.vo.SearchVo;
import org.dromara.business.modules.searchHistory.mapper.BuSearchHistoryMapper;
import org.dromara.business.modules.searchHistory.service.IBuSearchHistoryService;
import org.dromara.common.core.service.DictService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 用户搜索历史记录Service业务层处理
 *
 * @author ckk
 * @date 2025-11-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BuSearchHistoryServiceImpl implements IBuSearchHistoryService {

    private final BuSearchHistoryMapper baseMapper;
    private final DictService dictService;

    /**
     * 分页查询用户搜索历史记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户搜索历史记录分页列表
     */
    @Override
    public TableDataInfo<BuSearchHistoryVo> queryPageList(BuSearchHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BuSearchHistory> lqw = buildQueryWrapper(bo);
        lqw.eq(BaseEntity::getCreateBy, LoginHelper.getUserId());
        lqw.orderByDesc(BuSearchHistory::getCreateTime);
        Page<BuSearchHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<BuSearchHistory> buildQueryWrapper(BuSearchHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BuSearchHistory> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BuSearchHistory::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), BuSearchHistory::getKeyword, bo.getKeyword());
        return lqw;
    }

    @Override
    public TableDataInfo<SearchHistoryVo> hotSearch(PageQuery pageQuery) {
        Page<SearchHistoryVo> result = baseMapper.hotSearch(pageQuery.build());
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<SearchVo> search(String keyword, PageQuery pageQuery) {
        BuSearchHistory searchHistory = new BuSearchHistory();
        searchHistory.setKeyword(keyword);
        baseMapper.insert(searchHistory);
        Page<SearchVo> result = baseMapper.search(pageQuery.build(), keyword);
        result.getRecords().forEach(item -> {
            if (item.getCategoryId() != null) {
                item.setCategoryName(dictService.getDictLabel("xqfl", item.getCategoryId().toString()));
            }
        });
        return TableDataInfo.build(result);
    }
}
